package com.zhongmeng.rxjava2test

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView

import com.bigkoo.convenientbanner.ConvenientBanner
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator
import com.zhongmeng.rxjava2test.adapter.NoticeAdapter
import com.zhongmeng.rxjava2test.adapter.WorkNoticeAdapter
import com.zhongmeng.rxjava2test.bean.Banner
import com.zhongmeng.rxjava2test.bean.BaseBean
import com.zhongmeng.rxjava2test.bean.Notice
import com.zhongmeng.rxjava2test.bean.Work
import com.zhongmeng.rxjava2test.presenter.HomePresenter
import com.zhongmeng.rxjava2test.presenter.HomePresenterImpl
import com.zhongmeng.rxjava2test.utils.LogUtils
import com.zhongmeng.rxjava2test.utils.NetworkImageHolderView
import com.zhongmeng.rxjava2test.utils.SaveUserInfo
import com.zhongmeng.rxjava2test.utils.UIUtils
import com.zhongmeng.rxjava2test.view.HomeView

import butterknife.BindView
import butterknife.ButterKnife
import butterknife.OnClick
import kotterknife.bindView
import java.util.*

class HomeActivity : AppCompatActivity(), HomeView<Any> {

    val mTitle:TextView by bindView(R.id.text_title)
    val buttonBackward:Button by bindView(R.id.button_backward)
    val mBanner: ConvenientBanner<String> by bindView(R.id.banner)
    val more: ImageView by bindView(R.id.more)
    val noticeRecyclerview: RecyclerView by bindView(R.id.notice_recyclerview)

    /*@BindView(R.id.text_title)
    internal var mTitle: TextView? = null
    @BindView(R.id.button_backward)
    internal var buttonBackward: Button? = null
    @BindView(R.id.banner)
    internal var mBanner: ConvenientBanner<String>? = null
    @BindView(R.id.more)
    internal var more: ImageView? = null
    @BindView(R.id.notice_recyclerview)
    internal var noticeRecyclerview: RecyclerView? = null
    @BindView(R.id.work_rccyclerview)
    internal var workRccyclerview: RecyclerView? = null*/

    private var homePresenter: HomePresenter? = null

    private var networkImages: MutableList<String>? = null
    //private LoadingDialog loadingDialog;

    private var noticeAdapter: NoticeAdapter? = null
    private val workAdapter: WorkNoticeAdapter? = null
    private val noticeBean = ArrayList<Notice.NoticeListBean>()
    private var noticeListBean: ArrayList<Notice.NoticeListBean>? = null
    private var context: Context? = null
    private var platType: String? = null

    private val BANNER_WHICH = 1
    private val NOTICE_WHICH = 2
    private val WORK_WHICH = 3
    private val QINIU_WHICH = 4

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        context = applicationContext
        setContentView(R.layout.activity_home)
        ButterKnife.bind(this)
        init()
    }

    private fun init() {
        mTitle!!.text = "首页"
        buttonBackward!!.visibility = View.INVISIBLE
        platType = SaveUserInfo.instance.type
        networkImages = ArrayList()
        //loadingDialog = new LoadingDialog(context);

        homePresenter = HomePresenterImpl(this)
        homePresenter!!.getNoticeInfo(NOTICE_WHICH)
        homePresenter!!.getBannerUrls(BANNER_WHICH)
        homePresenter!!.getQiuniuToken(QINIU_WHICH)


        if (platType != "1") {//不为1时，加载待办工作数据
            homePresenter!!.getWorkInfo(WORK_WHICH)
        }

    }

    private fun initBanner() {
        //网络加载例子
        //networkImages = Arrays.asList(images);
        LogUtils.z("networkImages.size()=" + networkImages!!.size)
        if (networkImages!!.size != 0) {
            Handler().postDelayed({
                mBanner!!
                        .setPages(CBViewHolderCreator { NetworkImageHolderView() }, networkImages)
                        //设置两个点图片作为翻页指示器，不设置则没有指示器，可以根据自己需求自行配合自己的指示器,不需要圆点指示器可用不设
                        .setPageIndicator(intArrayOf(R.drawable.point_select, R.drawable.point_normal))
                        //设置指示器的方向
                        .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL)
            }, 10)
        }


    }

    // 开始自动翻页
    public override fun onResume() {
        super.onResume()
        //开始自动翻页
        mBanner!!.startTurning(2000)
    }

    // 停止自动翻页
    public override fun onPause() {
        super.onPause()
        //停止翻页
        mBanner!!.stopTurning()
    }

    override fun onDestroy() {
        super.onDestroy()
        homePresenter!!.onDestory()
    }


    override fun showProgress() {

    }

    override fun hideProgress() {

    }


    override fun onSuccess(data: Any, which: Int) {
        val code: Int
        when (which) {
            BANNER_WHICH -> {
                val banner = data as Banner
                code = banner.code
                LogUtils.z("bannercode=" + code)
                if (code == 200 && banner != null) {
                    val bannerData = banner.imgList
                    LogUtils.z("bannerData=" + bannerData!!.size)
                    for (imgListBean in bannerData) {
                        LogUtils.z(imgListBean.url!!)
                        networkImages!!.add(imgListBean.url!!)
                    }
                    initBanner()
                } else
                    UIUtils.toast(banner.message!!)
            }
            NOTICE_WHICH -> {
                val notice = data as Notice
                code = notice.code
                LogUtils.z("noticecode=" + code)
                if (code == 200) {
                    noticeListBean = notice.noticeList
                    if (noticeListBean!!.size > 4) {
                        more!!.visibility = View.VISIBLE
                        for (i in 0..3) {
                            noticeBean.add(noticeListBean!![i])
                        }
                    }
                    noticeAdapter = NoticeAdapter(this, R.layout.notice_recycler_item, noticeBean)
                    noticeRecyclerview!!.layoutManager = LinearLayoutManager(context)
                    noticeRecyclerview!!.adapter = noticeAdapter
                } else
                    UIUtils.toast(notice.message!!)
            }
            WORK_WHICH -> {
            }
            QINIU_WHICH -> {
                val baseBean = data as BaseBean
                LogUtils.z("code=" + baseBean.code)
                if (baseBean.code == 200) {
                    LogUtils.z("qiniu=" + baseBean.upToken!!)
                }
            }
        }

    }

    override fun onFail(throwable: Throwable) {
        UIUtils.toast(throwable.message!!)
        LogUtils.z(throwable.message!!)
    }
}
